<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Cimo\CompanyInvite;
use Carbon\Carbon;
use App\User;
use App\Models\Cimo\UserNeed;
use App\Models\Company;
use App\Models\Code;
use Illuminate\Support\Facades\Log;

class Invite extends Command
{
    protected $signature = 'invite:change';
    
    protected $description = '面试邀请定时任务.';
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function handle()
    {
        $inviteModel = new CompanyInvite();
        $invites = $inviteModel->all();
    
        //$this->info("\n面试邀请定时器开始...");
    
        $this->output->progressStart($invites->count());
    
        $nowtime = Carbon::now();
        $tomorrow = Carbon::tomorrow();
        foreach ($invites as $invite) {
            if($invite->interview_at !=0 ){
                $interview_at = Carbon::createFromTimestamp($invite->interview_at);
                $interviewTime = $interview_at->format('Y-m-d 00:00:00');
                if($interviewTime == $tomorrow) {
                    $resume = json_decode($invite->resume);
                    $iuser = User::find($invite->user_id);
                    $need = UserNeed::find($invite->need_id);
                    $company = Company::with('addressinfo','created_user')->find($invite->company_id);
                    $code = $this->codeStore($resume, $iuser, $need, $company);
                    
                    $this->call('emails:send', array('code_id' => $code->id));
                    
                    $this->call('msg:send',array('code_id' => $code->id));
                }
                if( $interview_at < $nowtime && $invite->status == 1) {
                    $invite->status = 4;
                }
            }
            //超过8小时未确认
            if((empty($invite->interview_at) || $invite->interview_at == 0 )
                && $invite->status == 0 && $invite->created_at < $nowtime->subHour(8)) {
                $invite->status = 99;
            }
                 $invite->save();
            
        }
        Log::info(['面试邀请相关的计划任务执行完毕！'=>'success']);   
    }
    
    private function codeStore($resume,$iuser,$need,$company)
    {
        $content = [
            'phone'=> $resume->phone,
            'user' => $iuser->real_name,
            'need'=>$need->name,
            'company'=>$company->name,
            'address'=>$company->addressinfo->address,
            'iphone' => $company->tel,
            'iuser'  => $company->created_user->real_name,
            'url'   => url('/company/detail?comid='.$company->id),
        ];
        
        $code = get_rnd_num(6);
        
        $code = $this->checkCode($code, $resume->email,1,89);
        
        $attributes = array(
            'code'      => $code,
            'phone'     => $resume->email,
            'content'   => json_encode($content),
            'is_email'  => 1,
            'type'      => 89,
            'status'    => 0,
            'is_used'   => 1,
        );
        
        return Code::firstOrCreate($attributes);
    }
    
    private function checkCode($code,$phone,$is_email = 0 , $type = 0)
    {
        $isHas = Code::where(['phone'=>$phone,'code'=>$code,'is_email'=>$is_email,'type'=>$type])->first();
    
        if(!empty($isHas))
        {
            $code = get_rnd_num(6);
    
            $this->checkCode($code,$phone,$is_email , $type);
        }
    
        return $code;
    }
}

